from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from movies.models import Movie, UserMoviePreferences


class Command(BaseCommand):
    """初始化电影数据和用户命令"""

    help = '向数据库中添加中文模拟电影数据和用户'

    def handle(self, *args, **options):
        """处理命令执行"""
        # 创建用户
        self.create_users()

        # 创建电影数据
        self.create_movies()

        # 输出总结信息
        self.stdout.write(
            self.style.SUCCESS(
                '数据初始化完成：用户和电影数据已创建'
            )
        )

    def create_users(self):
        """创建示例用户"""
        User = get_user_model()

        # 定义要创建的用户数据
        users_data = [
            {
                "username": "admin",
                "email": "admin@example.com",
                "password": "admin123",
                "is_staff": True,
                "is_superuser": True
            },
            {
                "username": "user1",
                "email": "user1@example.com",
                "password": "user123"
            },
            {
                "username": "user2",
                "email": "user2@example.com",
                "password": "user123"
            }
        ]

        # 创建用户
        created_users = 0
        for user_data in users_data:
            # 检查用户是否已存在
            if not User.objects.filter(username=user_data["username"]).exists():
                # 创建用户
                password = user_data.pop("password")
                user = User.objects.create_user(**user_data)
                user.set_password(password)
                user.save()

                # 为普通用户创建电影偏好设置
                if not user_data.get("is_superuser", False):
                    UserMoviePreferences.objects.get_or_create(user=user)

                created_users += 1
                self.stdout.write(
                    self.style.SUCCESS(f'成功创建用户: {user_data["username"]}')
                )
            else:
                self.stdout.write(
                    f'用户已存在: {user_data["username"]}'
                )

        self.stdout.write(
            self.style.SUCCESS(
                f'共创建了 {created_users} 个新用户'
            )
        )

    def create_movies(self):
        """创建示例电影数据"""
        # 定义要插入的电影数据
        movies_data = [
            {
                "title": "肖申克的救赎",
                "genres": ["剧情", "犯罪"],
                "country": "美国",
                "release_year": 1994,
                "extra_data": {"directors": ["弗兰克·德拉邦特"], "actors": ["蒂姆·罗宾斯", "摩根·弗里曼"]}
            },
            {
                "title": "霸王别姬",
                "genres": ["剧情", "爱情"],
                "country": "中国",
                "release_year": 1993,
                "extra_data": {"directors": ["陈凯歌"], "actors": ["张国荣", "张丰毅", "巩俐"]}
            },
            {
                "title": "阿甘正传",
                "genres": ["剧情", "爱情"],
                "country": "美国",
                "release_year": 1994,
                "extra_data": {"directors": ["罗伯特·泽米吉斯"], "actors": ["汤姆·汉克斯", "罗宾·怀特", "加里·西尼斯"]}
            },
            {
                "title": "泰坦尼克号",
                "genres": ["剧情", "爱情"],
                "country": "美国",
                "release_year": 1997,
                "extra_data": {"directors": ["詹姆斯·卡梅隆"], "actors": ["莱昂纳多·迪卡普里奥", "凯特·温斯莱特"]}
            },
            {
                "title": "这个杀手不太冷",
                "genres": ["剧情", "动作"],
                "country": "法国",
                "release_year": 1994,
                "extra_data": {"directors": ["吕克·贝松"], "actors": ["让·雷诺", "娜塔莉·波特曼", "加里·奥德曼"]}
            },
            {
                "title": "美丽人生",
                "genres": ["剧情", "喜剧"],
                "country": "意大利",
                "release_year": 1997,
                "extra_data": {"directors": ["罗伯托·贝尼尼"], "actors": ["罗伯托·贝尼尼", "尼可莱塔·布拉斯基", "乔治·坎塔里尼"]}
            },
            {
                "title": "千与千寻",
                "genres": ["动画", "奇幻"],
                "country": "日本",
                "release_year": 2001,
                "extra_data": {"directors": ["宫崎骏"], "actors": ["柊瑠美", "入野自由", "夏木真理"]}
            },
            {
                "title": "辛德勒的名单",
                "genres": ["剧情", "历史"],
                "country": "美国",
                "release_year": 1993,
                "extra_data": {"directors": ["史蒂文·斯皮尔伯格"], "actors": ["连姆·尼森", "本·金斯利", "拉尔夫·费因斯"]}
            }
        ]

        # 插入电影数据
        created_count = 0
        for movie_data in movies_data:
            # 检查是否已存在相同标题的电影
            if not Movie.objects.filter(title=movie_data["title"]).exists():
                Movie.objects.create(**movie_data)
                created_count += 1
                self.stdout.write(
                    self.style.SUCCESS(f'成功创建电影: {movie_data["title"]}')
                )
            else:
                self.stdout.write(
                    f'电影已存在: {movie_data["title"]}'
                )

        self.stdout.write(
            self.style.SUCCESS(
                f'共创建了 {created_count} 条新电影记录'
            )
        )
